import requests
from bs4 import BeautifulSoup

# headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'}
# r = requests.get('http://www.myeln.com.cn/', headers = headers)
# content = r.text
# 把刚刚保存在content中的文件放入Beautiful Soup中
f = open('a.html')
content=f.read()
soup = BeautifulSoup(content, 'lxml')
tree = soup.find_all(class_ = 'tree_cata')
dls=tree[0].find_all('dl')
dds=dls[0].find_all("dd")

result="["
for dd in dds:
    
    h5=dd.find("h5")
    if(h5==None):
        result=result+"  {"
        top=dd.find("h2").find("a")
        result=result+'"name": "'+top.get_text().strip()+'",'+'"sub": ['
        allA=dd.find("div").find("h4").find_all("a")
        for a in allA:
            result=result+"  {"
            result=result+'"name": "'+a.get_text().strip()+'"'
            result=result+"  },"
        result=result[:-1]
        result=result+"  ]"
        result=result+"  },"
    else:
        topMore=dd.find("div").find("h4")
        aType=topMore.find_all("a")
        isFirst=True
        for a in aType:
            top=a.find("b")
            if(top!=None):
                if(isFirst):
                    result=result+"  {"
                    result=result+'"name": "'+top.get_text().strip()+'",'+'"sub": ['
                    isFirst=False
                else:
                    result=result[:-1]
                    result=result+"  ]"
                    result=result+"  },"
                    result=result+"  {"
                    result=result+'"name": "'+top.get_text().strip()+'",'+'"sub": ['
            else:
                result=result+"  {"
                result=result+'"name": "'+a.get_text().strip()+'"'
                result=result+"  },"
result=result[:-1]
result=result+"  ]"
print(result)